package lc04;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @Author Stringzhua
 * @Date 2024/4/18 20:03
 * description:
 */
public class test03 {
    @Test
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int[] array = new int[nums1.length + nums2.length];
        //2.1按照最小长度合并两个有序数组
        int p1 = 0, p2 = 0, i = 0;
        while (p1 < nums1.length && p2 < nums2.length) {
            if (nums1[p1] < nums2[p2]) {
                array[i++] = nums1[p1++];
            } else {
                array[i++] = nums2[p2++];
            }
        }
        //2.2复制剩余元素
        if (p1 < nums1.length) {
            System.arraycopy(nums1, p1, array, i, nums1.length - p1);
        } else {
            System.arraycopy(nums2, p2, array, i, nums2.length - p2);
        }
        //冒泡排序
        //冒泡排序
        for (int m = 0, n = array.length; m < n - 1; m++) {
            for (int k = 0; k < n - 1 - m; k++) {
                if (array[k] > array[k + 1]) {
                    //交换
                    int temp = array[k];
                    array[k] = array[k + 1];
                    array[k + 1] = temp;
                }
            }
        }
        int mid = array.length / 2;
        if (array.length % 2 == 0) {
            return (double) ((array[mid - 1] + array[mid]) / 2.0);
        } else {
            return (double) array[mid];
        }
    }

    public static void main(String[] args) {
        int[] nums1 = {1, 3};
        int[] nums2 = {2};
        test01 test01 = new test01();
        double[] array = test01.findMedianSortedArrays(nums1, nums2);
        System.out.println(Arrays.toString(array));
    }
}
